<?php
/**
 * +----------------------------------------------------------------------
 * | Copyright © Sogu Technologies Co. Ltd 2018-2050 All rights reserved.
 * +----------------------------------------------------------------------
 * | File name: Ding.php
 * +----------------------------------------------------------------------
 * | Author: sam
 * +----------------------------------------------------------------------
 * | Description: 用于详细说明此程序文件完成的主要功能，与其他模块或函数的接口、输出值、取值范围、含义及参数间的控制、顺序或依赖等关系。
 * +----------------------------------------------------------------------
 * | Others: 其他内容的说明
 * +----------------------------------------------------------------------
 * | History: 修改历史记录列表，每条修改记录应包含修改日期、修改者及修改内容简述
 * +----------------------------------------------------------------------
 * | 1.Date: 2019/1/5 Author: sam Modification: 创建
 * +----------------------------------------------------------------------
 */

namespace App\Library\Ding;


use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;

class Ding
{
    CONST NOTICE_URI = "https://oapi.dingtalk.com/";
    CONST NOTICE_URL = "robot/send?access_token=7bb311cd33f69d80e2ce12eb337abd1fe42ae6548b44a9fe4461c025abad45dc";
    CONST PRO_URL = "robot/send?access_token=653985810fd23899b99d00da71c8ad67a26ca4e4d66b86585cad4dec179f4f94";

    /**
     * Description: 钉钉文本通知
     * Author: Gxs
     * @param $data
     * @param array $at
     * @param bool $isAtAll
     * @param string $env
     */
    public static function text($data, $at = [], $isAtAll = false, $env = 'local')
    {
        $base_uri = self::NOTICE_URI;
        if ('Pro' == $env) {
            $notice_url = self::PRO_URL;
        } else {
            $notice_url = self::NOTICE_URL;
        }
        $client = new Client(['base_uri' => $base_uri,'timeout' => 3.0]);
        $text_array = [
            'msgtype' => 'text',
            'text' => ['content' => $data],
            'at' => ['atMobiles' => $at, 'isAtAll' => $isAtAll],
        ];

        $res = $client->request('POST', $notice_url,[ 'headers' => ['Content-Type'=>'application/json;charset=utf-8',],
                'json'=>$text_array,
                'verify'=>false]);
        $result = json_decode($res->getBody()->getContents(), true);
        if ($result['errmsg'] != 'ok' || $result['errcode'] != 0)
            Log::debug('ding_ding_text_message_failure', ['result' => $result]);
    }

    /**
     * Description: 钉钉预警markdown
     * Author: Gxs
     * @param $data
     * @param array $at
     * @param bool $isAtAll
     * @param string $env
     */
    public static function markdown($data, $at = [], $isAtAll = false, $env = 'local')
    {
        $base_uri = self::NOTICE_URI;
        if ('Pro' == $env) {
            $notice_url = self::PRO_URL;
        } else {
            $notice_url = self::NOTICE_URL;
        }

        $client = new Client(['base_uri' => $base_uri, 'timeout' => 3.0]);
        $makdown_array = [
            'msgtype' => 'markdown',
            'markdown' => $data,
            'at' => ['atMobiles' => $at, 'isAtAll' => $isAtAll],
        ];
        $res = $client->request('POST',
            $notice_url,
            [ 'headers' => ['Content-Type'=>'application/json;charset=utf-8',],
                'json'=>$makdown_array,
                'verify'=>false]);
        $result = json_decode($res->getBody()->getContents(), true);
        if ($result['errmsg'] != 'ok' && $result['errcode'] != 0)
            Log::debug('ding_ding_markdown_message_failure', ['result' => $result]);
    }
}